package cn.zlp.pet.service.impl;

import cn.zlp.basic.exception.BusinessException;
import cn.zlp.basic.service.impl.BaseServiceImpl;
import cn.zlp.basic.util.*;
import cn.zlp.order.domain.PetAcquisitionOrder;
import cn.zlp.order.mapper.PetAcquisitionOrderMapper;
import cn.zlp.org.domain.Employee;
import cn.zlp.org.domain.Shop;
import cn.zlp.org.mapper.EmployeeMapper;
import cn.zlp.org.mapper.ShopMapper;
import cn.zlp.pet.domain.Pet;
import cn.zlp.pet.domain.SearchMasterMsg;
import cn.zlp.pet.mapper.SearchMasterMsgMapper;
import cn.zlp.pet.query.SearchMasterMsgQuery;
import cn.zlp.pet.service.IPetService;
import cn.zlp.pet.service.ISearchMasterMsgService;
import cn.zlp.user.domain.Logininfo;
import cn.zlp.user.domain.User;
import cn.zlp.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired(required = false)
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private ShopMapper shopMapper;

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;

    @Autowired(required = false)
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public List<SearchMasterMsg> getAll() {
        return searchMasterMsgMapper.loadAll();
    }

    @Override
    public void publish(SearchMasterMsg msg, HttpServletRequest request) {
        // 1.非空校验
        if (msg.getTitle().isEmpty() || msg.getAddress().isEmpty()){
            throw new BusinessException("标题和地址不能为空!!!");
        }
        // 2.获取登录用户的信息
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        msg.setUser(user);
        msg.setUser_id(user.getId());
        //3.通过工具类获取到最近的店铺的id
        List<Shop> shops = shopMapper.loadAll();
        Point point = DistanceUtil.getPoint(msg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        msg.setShop(nearestShop);
        msg.setShop_id(nearestShop.getId());
        //4.添加寻主消息
        searchMasterMsgMapper.save(msg);
        //5.给店铺管理员发送短信 - 作弊
        Employee employee = employeeMapper.loadById(nearestShop.getAdmin_id());
        //SmsUtil.sendMsg(employee.getPhone(),"你有新的订单，请到【" + msg.getAddress() + "】去收取宠物" + msg.getName());
        System.out.println("你有新的订单，请到【" + msg.getAddress() + "】去收取宠物" + msg.getName());
    }

    /**
     * 待处理分页查询
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        // 设置状态为待处理
        searchMasterMsgQuery.setState(0);
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null){ // 店铺id不为null,说明为店铺管理员
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        return super.pageQuery(searchMasterMsgQuery);
    }

    /**
     * 已处理分页查询
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        // 设置状态为已处理
        searchMasterMsgQuery.setState(1);
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null){ // 店铺id不为null,说明为店铺管理员
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        return super.pageQuery(searchMasterMsgQuery);
    }

    /**
     * 用户分页查询
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        //用户查询所有消息：待处理 + 已处理
        //获取登录的管理员信息：平台 or 店铺
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        //根据登录信息id 查询 管理员
        User user = userMapper.findByLogininfoId(logininfo.getId());
        searchMasterMsgQuery.setUserId(user.getId());

        return super.pageQuery(searchMasterMsgQuery);
    }

    @Override
    public void handle(Pet pet, HttpServletRequest request) {
        //1.待处理变成以处理消息 - 修改state=1通过 待处理消息的id
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());
        //2.添加宠物相关的信息
        petService.add(pet,request);
        //2.1.添加宠物信息
        //2.2.添加宠物明细
        // 3.创建订单
        // 3.1获取寻主消息
        SearchMasterMsg masterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        // 获取店铺管理员登录信息
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        // 根据登录id获取员工对象
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        // 创建收购订单
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, masterMsg, employee.getId());
        // 在数据库中保存收购订单
        petAcquisitionOrderMapper.save(petAcquisitionOrder);
        // 4.支付@todo
    }
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg masterMsg,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！"); // 设置摘要
        order.setState(0); // 待支付
        order.setPrice(pet.getCostprice()); // 设置收购价
        order.setAddress(masterMsg.getAddress()); // 设置收购地址
        String orderSn = CodeGenerateUtils.generateOrderSn(masterMsg.getUser_id()); // 生成订单号
        order.setOrderSn(orderSn); // 设置订单号
        order.setPet_id(pet.getId()); // 设置宠物id
        order.setUser_id(masterMsg.getUser_id()); // 设置寻主用户id
        order.setPaytype(pet.getPayType()); // 设置支付方式
        order.setShop_id(pet.getShop_id()); // 设置收购店铺id
        order.setEmp_id(empId); // 设置收购店铺员工id
        return order;
    }

}
